﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace AsteroidWar
{
    class CollisionManager
    {
        private int bossExplosion = 0;
        private AsteroidManager asteroidManager;
        private PlayerManager playerManager;
        private SecondPlayer secondPlayer;
        private EnemyManager enemyManager;
        private ExplosionManager explosionManager;
        bool special = true;
        private Vector2 offScreen = new Vector2(-500, -500);
        private Vector2 shotToAsteroidImpact = new Vector2(0, -20);
        private Boss boss;
        private int enemyPointValue = 100;

        public void setBoss(Boss boss)
        {
            this.boss = boss;
        }
        public CollisionManager(
            AsteroidManager asteroidManager,
            PlayerManager playerManager,
            SecondPlayer secondPlayer,
            EnemyManager enemyManager,
            ExplosionManager explosionManager, Boss boss)
        {
            this.asteroidManager = asteroidManager;
            this.playerManager = playerManager;
            this.enemyManager = enemyManager;
            this.explosionManager = explosionManager;
            this.boss = boss;
            this.secondPlayer = secondPlayer;
            
        }
        private void checkShotToBossCollisions()
        {
            if (!playerManager.Destroyed)
            {
                foreach (Sprite shot in playerManager.PlayerShotManager.Shots)
                {
                    
                        if (shot.IsCircleColliding(
                            boss.sprite.Center,
                            boss.sprite.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            boss.health -= playerManager.dame;
                            playerManager.PlayerScore += 20;
                            if (boss.health <= 0)
                            {
                                playerManager.PlayerScore += 1000;
                                explosionManager.AddExplosion(boss.sprite.Center, boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(52, 27), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-35, -440), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-15, -22), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-49, 33), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(77, 649), boss.sprite.Velocity / 10);
                                bossExplosion = 1;
                                                     
                            }
                            else
                            explosionManager.AddExplosion(
                                boss.sprite.Center,
                                boss.sprite.Velocity / 10);
                        }

                    


                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Sprite shot in secondPlayer.PlayerShotManager.Shots)
                {

                    if (shot.IsCircleColliding(
                        boss.sprite.Center,
                        boss.sprite.CollisionRadius))
                    {
                        shot.Location = offScreen;
                        boss.health -= secondPlayer.dame;
                        playerManager.PlayerScore += 20;
                        if (boss.health <= 0)
                            {
                                playerManager.PlayerScore += 1000;
                                explosionManager.AddExplosion(boss.sprite.Center, boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(52, 27), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-35, -440), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-15, -22), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-49, 33), boss.sprite.Velocity / 10);
                                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(77, 649), boss.sprite.Velocity / 10);
                                bossExplosion = 1;
                                                     
                            }
                            else
                            explosionManager.AddExplosion(
                                boss.sprite.Center,
                                boss.sprite.Velocity / 10);
                    }



                }
            }
        }
        private void checkShotToEnemyCollisions()
        {
            if (!playerManager.Destroyed)
            {
                foreach (Sprite shot in playerManager.PlayerShotManager.Shots)
                {
                    foreach (Enemy enemy in enemyManager.enemies)
                    {
                        if (shot.IsCircleColliding(
                            enemy.sprite.Center,
                            enemy.sprite.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            enemy.Destroyed = true;
                            playerManager.PlayerScore += enemyPointValue;
                            explosionManager.AddExplosion(
                                enemy.sprite.Center,
                                enemy.sprite.Velocity / 10);
                        }

                    }


                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Sprite shot in secondPlayer.PlayerShotManager.Shots)
                {
                    foreach (Enemy enemy in enemyManager.enemies)
                    {
                        if (shot.IsCircleColliding(
                            enemy.sprite.Center,
                            enemy.sprite.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            enemy.Destroyed = true;
                            secondPlayer.PlayerScore += enemyPointValue;
                            explosionManager.AddExplosion(
                                enemy.sprite.Center,
                                enemy.sprite.Velocity / 10);
                        }

                    }


                }
            }
        }

        private void checkShotToAsteroidCollisions()
        {
            if (!playerManager.Destroyed)
            {
                foreach (Sprite shot in playerManager.PlayerShotManager.Shots)
                {
                    foreach (Sprite asteroid in asteroidManager.Asteroids)
                    {
                        if (shot.IsCircleColliding(
                            asteroid.Center,
                            asteroid.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            asteroid.Velocity += shotToAsteroidImpact;
                        }
                    }
                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Sprite shot in secondPlayer.PlayerShotManager.Shots)
                {
                    foreach (Sprite asteroid in asteroidManager.Asteroids)
                    {
                        if (shot.IsCircleColliding(
                            asteroid.Center,
                            asteroid.CollisionRadius))
                        {
                            shot.Location = offScreen;
                            asteroid.Velocity += shotToAsteroidImpact;
                        }
                    }
                }
            }
        }

        private void checkShotToPlayerCollisions()
        {
            if (!playerManager.Destroyed)
            {
                foreach (Sprite shot in enemyManager.enemyShotManager.Shots)
                {
                    if (shot.IsCircleColliding(
                        playerManager.playerSprite.Center,
                        playerManager.playerSprite.CollisionRadius))
                    {
                        shot.Location = offScreen;
                        playerManager.Destroyed = true;
                        explosionManager.AddExplosion(
                            playerManager.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Sprite shot in enemyManager.enemyShotManager.Shots)
                {
                    if (shot.IsCircleColliding(
                        secondPlayer.playerSprite.Center,
                        secondPlayer.playerSprite.CollisionRadius))
                    {
                        shot.Location = offScreen;
                        secondPlayer.Destroyed = true;
                        explosionManager.AddExplosion(
                            secondPlayer.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }
        }

        private void checkEnemyToPlayerCollisions()
        {
            if (!playerManager.Destroyed)
            {
                foreach (Enemy enemy in enemyManager.enemies)
                {
                    if (enemy.sprite.IsCircleColliding(
                        playerManager.playerSprite.Center,
                        playerManager.playerSprite.CollisionRadius))
                    {
                        enemy.Destroyed = true;
                        explosionManager.AddExplosion(
                            enemy.sprite.Center,
                            enemy.sprite.Velocity / 10);

                        playerManager.Destroyed = true;

                        explosionManager.AddExplosion(
                            playerManager.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Enemy enemy in enemyManager.enemies)
                {
                    if (enemy.sprite.IsCircleColliding(
                        secondPlayer.playerSprite.Center,
                        secondPlayer.playerSprite.CollisionRadius))
                    {
                        enemy.Destroyed = true;
                        explosionManager.AddExplosion(
                            enemy.sprite.Center,
                            enemy.sprite.Velocity / 10);

                        secondPlayer.Destroyed = true;

                        explosionManager.AddExplosion(
                            secondPlayer.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }
        }

        private void checkAsteroidToPlayerCollisions()
        {
            if (!playerManager.Destroyed)
            {

                foreach (Sprite asteroid in asteroidManager.Asteroids)
                {
                    if (asteroid.IsCircleColliding(
                        playerManager.playerSprite.Center,
                        playerManager.playerSprite.CollisionRadius))
                    {
                        explosionManager.AddExplosion(
                            asteroid.Center,
                            asteroid.Velocity / 10);

                        asteroid.Location = offScreen;

                        playerManager.Destroyed = true;
                        explosionManager.AddExplosion(
                            playerManager.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                foreach (Sprite asteroid in asteroidManager.Asteroids)
                {
                    if (asteroid.IsCircleColliding(
                        secondPlayer.playerSprite.Center,
                        secondPlayer.playerSprite.CollisionRadius))
                    {
                        explosionManager.AddExplosion(
                            asteroid.Center,
                            asteroid.Velocity / 10);

                        asteroid.Location = offScreen;

                        secondPlayer.Destroyed = true;
                        explosionManager.AddExplosion(
                            secondPlayer.playerSprite.Center,
                            Vector2.Zero);
                    }
                }
            }

        }
        private void checkBossToPlayerCollisions()
        {
            if (!playerManager.Destroyed)
            {

                if (boss.sprite.IsCircleColliding(playerManager.playerSprite.Center, playerManager.playerSprite.CollisionRadius))
                // if (boss.bossSprite.IsBoxColliding(playerManager.playerSprite.BoundingBoxRect))
                {
                    //explosionManager.AddExplosion(boss.bossSprite.Center,boss.bossSprite.Velocity/10);
                    playerManager.Destroyed = true;

                    explosionManager.AddExplosion(
                           playerManager.playerSprite.Center,
                           Vector2.Zero);
                    playerManager.playerSprite.Location = offScreen;
                }
            }
            if (GameSettings._2players && !secondPlayer.Destroyed)
            {
                if (boss.sprite.IsCircleColliding(secondPlayer.playerSprite.Center, secondPlayer.playerSprite.CollisionRadius))
                // if (boss.bossSprite.IsBoxColliding(playerManager.playerSprite.BoundingBoxRect))
                {
                    //explosionManager.AddExplosion(boss.bossSprite.Center,boss.bossSprite.Velocity/10);
                    secondPlayer.Destroyed = true;

                    explosionManager.AddExplosion(
                           secondPlayer.playerSprite.Center,
                           Vector2.Zero);
                    secondPlayer.playerSprite.Location = offScreen;
                }
            }
        }
        public void CheckCollisions()
        {
            if (bossExplosion==1)
            {
                explosionManager.AddExplosion(boss.sprite.Center, boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(20, 20), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, 30), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, -20), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-40, 40), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(50, 50), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-100, 30), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, -40), boss.sprite.Velocity / 10);
                explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-20, 60), boss.sprite.Velocity / 10);
                bossExplosion = 2;
            }
            else
                if (bossExplosion == 2)
                {
                    explosionManager.AddExplosion(boss.sprite.Center, boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(20, 20), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, 30), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, -20), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-40, 40), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(50, 60), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-70, 30), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, -20), boss.sprite.Velocity / 10);
                    explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-20, 60), boss.sprite.Velocity / 10);
                    bossExplosion = 3;
                }
                else
                    if (bossExplosion == 3)
                    {
                        explosionManager.AddExplosion(boss.sprite.Center, boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(50, 20), boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-60, 30), boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-10, -30), boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-20, 60), boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(50, 80), boss.sprite.Velocity / 10);

                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(-20, 60), boss.sprite.Velocity / 10);
                        explosionManager.AddExplosion(boss.sprite.Center + new Vector2(20, -50), boss.sprite.Velocity / 10);
                        bossExplosion = 0;
                        boss.Destroyed = true;
                    }
            
          
            checkShotToEnemyCollisions();
            checkShotToAsteroidCollisions();
            checkBossToPlayerCollisions();
            checkShotToPlayerCollisions();
            checkEnemyToPlayerCollisions();
            checkAsteroidToPlayerCollisions();
            checkShotToBossCollisions();
        }

    }
}
